﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Savorboard.CAP.InMemoryMessageQueue;
using static System.Runtime.InteropServices.JavaScript.JSType;

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");

        //CreateHostBuilder(args).Build().Run();
        RunWebHost(args);


    }

    static void CreateHostBuilder(string[] args)
    {
        var build = new HostBuilder();
        build.ConfigureServices((hostContext, services) =>
        {
            services.AddCap(option =>      //添加CAP框架
            {
                option.UseInMemoryStorage();   //使用内存存储
                option.UseInMemoryMessageQueue();  //使用内存队列
                option.UseDashboard();  //添加监控仪表盘
            });

            services.AddHostedService<Publish>();

            services.AddSingleton<ICapSubscribe, Events>();
        });
        build.Build().Run();
    }

    static void RunWebHost(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddHostedService<Publish>();

        builder.Services.AddSingleton<ICapSubscribe, Events>();

        builder.Services.AddCap(option =>      //添加CAP框架
        {
            //option.UseInMemoryStorage();   //使用内存存储
            //option.UseInMemoryMessageQueue();  //使用内存队列
            option.UseDashboard();  //添加监控仪表盘  默认地址ip:端口/cap
            //mysql做信息载体
            option.UseMySql("Data Source=localhost;Database=captest;User ID=root;Password=123456;pooling=true;port=3306;sslmode=none;CharSet=utf8;Convert Zero Datetime=True;Allow Zero Datetime=True;");
            //mq做broker
            option.UseRabbitMQ(opt => { 
                opt.HostName = "101.35.232.57";
                opt.Port = 5672;
                opt.VirtualHost = "magic";
                opt.UserName = "swsk33";
                opt.Password = "123456";
            });
        });

     

        var app = builder.Build();
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {

        });
        app.Run("http://*:9090");
    }
}

public class Publish : IHostedService
{
    private ICapPublisher capPublish;  //CAP发布程序
    private Timer _timer;   //定时器

    public Publish(ICapPublisher capPublish)  //自动注入CAP程序
    {
        this.capPublish = capPublish;
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _timer = new Timer(work, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));  //每秒运行一次推送任务
        return Task.CompletedTask;
    }
    private void work(object state)
    {
        capPublish.Publish<string>("Order.Created", $"你好：{DateTime.Now.ToString()}");  //推送Order.Created事件
        Console.WriteLine("推送：" + DateTime.Now.ToString());
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}


public class Events : ICapSubscribe
{


    [CapSubscribe("Order.Created")]   //监听Order.Created事件
    public async Task OrderCreatedEventHand(string msg)
    {
        Console.WriteLine("--接收：" + msg);
    }

}